//输入内容提示消失
$(function(){
    $("#phonenumber").bind('input propertychange', function() {
        $("#word1").html("");
    });
    $("#testnum").bind('input propertychange', function() {
        $("#word2").html("");
    });
    $("#newpw").bind('input propertychange', function() {
        $("#word3").html("");
    });
});
var reg=/0?(13|14|15|18)[0-9]{9}$/;
var userid; //保存进行密码修改的用户ID
var md5phonenumber;
//获取验证码方法
function gettestnumber(){
    $("#word2").html("");
    var phonenumber=document.getElementById("phonenumber").value;
    clearInterval();
    $.ajax({
        type:"get",
        dataType: "json",
        //url:"https://www.pulu.com/webapi/api/Sms/GetValidCode?phoneNum="+phonenumber,
        url:"https://www.pulu.com/webapi/api/Sms/GetValidCode?phoneNum="+phonenumber,
        success:function(data){
            console.log(data);
            if(data.Code==="FALSE"){
                $("#word2").html("验证码发送失败");
            }else{
                var clocktim=60;
                $("#time").html(clocktim);
                $("#getphonetest").css("display","none");
                $("#time").css("display","block");
                var secondtime=setInterval(function(){
                    if(clocktim>0){
                        clocktim--;
                        $("#time").html(clocktim);
                    }else{
                        clearInterval(secondtime);
                        $("#getphonetest").css("display","block");
                        $("#time").css("display","none");
                        clocktim=60;
                    }
                },1000);
            }
        }
    });
}
//$("#getphonetest").click(function(){
$("#phonenextbtn").click(function(){
    var phonenumber=document.getElementById("phonenumber").value;
    if(!reg.test(phonenumber)){
        $("#word1").html("手机号格式有误");
    }else{
        //是否已经注册
        md5phonenumber=hex_md5(phonenumber).substring(7, 23);
        $.ajax({
            type:"post",
            dataType: "json",
            url:"https://www.pulu.com/webapi/api/puluuser/getuser",
            data:{"userId":md5phonenumber},
            success:function(d){
                console.log(d);
                //如果存在帐号
                if(d.Code==="TRUE"){
                    //给手机发送验证码
                    gettestnumber();
                    $("#inputphone").css("display","none");
                    $("#testnumber").css("display","block");
                }else{
                    $("#nouser").css("display","block");
                }
            }
        });

    }
});
$("#changgepn").click(function(){
    $("#nouser").css("display","none");
});
$("#gotozc").click(function(){
    location.href="index.html";
});
//再次获取验证码
$("#getphonetest").click(gettestnumber);
//验证码下一页
$("#testnextbtn").click(function(){
    var phonenumber=document.getElementById("phonenumber").value;
    var testnum=document.getElementById("testnum").value;
    if(document.getElementById("testnum").value&&document.getElementById("testnum").value.length>0){
            $.ajax({
                type:"get",
                dataType: "json",
                url:"https://www.pulu.com/webapi/api/Sms/Validate?phoneNum="+phonenumber+"&code="+testnum,
                success:function(d){
                    console.log(d);
                    //验证成功
                    if(d.Message==="验证码正确"){
                        $("#testnumber").css("display","none");
                        $("#newpassword").css("display","block");
                    }
                    //验证失败
                    else{
                        $("#word2").html("验证码错误");
                    }
                }
            })

    }else{
        $("#word2").html("验证码错误");
    }
});
//最后重置
$("#changepwbtn").click(function(){
    if(/\s/.test($("#newpw").val())||($("#newpw").val()).length<6||($("#newpw").val()).length>12){
        $("#word3").html("请输入6到12位有效密码");
    }else{
        if(/[0-9]{6,12}|[a-z]{6,12}|[A-Z]{6,12}|[0-9a-zA-Z]{6,12}/.test($("#newpw").val())){
            var newpw=document.getElementById("newpw").value;
            var md5password=hex_md5(newpw).substring(7, 23);
            $.ajax({
                type:"post",
                dataType: "json",
                url:"https://www.pulu.com/webapi/api/PuluUser/UpdatePw",
                data:{"userId":md5phonenumber,"pw":md5password},
                success:function(data){
                    console.log(data);
                    $.cookie("MYID",md5phonenumber);
                    $("#word3").html("修改成功");
                    //微信
                    if($.cookie("wxopenid")){
                        $.ajax({
                            type:"post",
                            url:"https://www.pulu.com/webapi/api/WebChatInfo/AddWebChatInfo",
                            data:{"userId":md5phonenumber,"webchatId":$.cookie("wxopenid")},
                            success:function(d){
                                location.href="login.html";
                            }
                        })
                    }
                    setTimeout(function(){
                        location.href="login.html";
                    },1000)
                }
            });
        }else{
            $("#word3").html("请输入6到12位密码");
        }
    }

});
//个页面返回
$("#inputphone .mweb_modal_back_button").click(function(){
     history.go(-1);
});
$("#testnumber .mweb_modal_back_button").click(function(){
    $("#inputphone").css("display","block");
    $("#testnumber").css("display","none");
});
$("#newpassword .mweb_modal_back_button").click(function(){
    $("#testnumber").css("display","block");
    $("#newpassword").css("display","none");
});